package cn.liontalk.config;

import cn.liontalk.annotation.UserContext;
import cn.liontalk.entity.User;
import cn.liontalk.service.UserService;
import cn.liontalk.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 周哲
 * @package: cn.liontalk.config
 * @description:
 * @date: 2019/1/20 13:15
 * @version: V1.0
 */
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {


    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        Class<?> clazz = methodParameter.getParameterType();
        return clazz == User.class;
    }


    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletResponse response = (HttpServletResponse) webRequest.getNativeResponse();
        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

        User user = UserContext.getUser();
        return user;
    }
}
